﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Apps.Data.Project;
using EzDesk.Apps.Interface.Project;
using EzDesk.Apps.Model.Project;
using EzDesk.Utility.ServiceStatusInfo;

namespace EzDesk.Apps.ServiceImpl.Project
{
    public partial class ProjectService : IProjectService
    {
        public StatusInfo<Guid> AddWorkitem(Workitem workItem)
        {
            return StatusInfo.Wrap(
                () =>
                    {
                        var newWorkitemId = Guid.NewGuid();
                        workItem.Id = newWorkitemId;
                        EzDeskProjectContainer.GoAndSave(
                            entity => entity.WorkitemSet.AddObject(workItem));
                        return newWorkitemId;
                    });
        }


        public StatusInfo<Workitem> GetWorkitem(Guid workitemId)
        {
            return StatusInfo.Wrap(
                () =>
                    {
                        var item = EzDeskProjectContainer.Go(
                            entity => entity.WorkitemSet.FirstOrDefault(p => p.Id == workitemId));
                        if (item == null)
                        {
                            throw new Exception("没有找到对应的工作任务");
                        }
                        return item;
                    });
        }
    }
}
